﻿using MiniExcelLibs;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace Synergy
{
    public partial class Form不良原因汇总表 : Form
    {
        public Form不良原因汇总表()
        {
            InitializeComponent();
        }

        private void button刷新_Click(object sender, EventArgs e)
        {
            if (checkBox显示人员.Checked == true)
            {
                刷新包含人员();
            }
            else
            {
                刷新();
            }
        }

        private void 刷新()
        {
            string sql = @"select  '' 序号,[工序名称],[不良原因大类名称] 不良大类 ,[不良原因名称] 不良原因,CAST(sum([数量]) as int) [数量]
                        from Z_报工不良原因
                         where 1=1";
            if (dateTimePicker_ksrq.Checked == true)
            {
                sql += $"and 质检时间 > = '{dateTimePicker_ksrq.Value.ToString("yyyy-MM-dd")}'";
            }
            if (dateTimePicker_jsrq.Checked == true)
            {
                sql += $"and 质检时间 < '{dateTimePicker_jsrq.Value.AddDays(1).ToString("yyyy-MM-dd")}'";
            }
            sql += " group by [不良原因大类名称],[不良原因名称],[工序名称] order by 数量 desc";
            DataTable dt = SqlSugarHelp.scope.Ado.GetDataTable(sql);
            //增加序号
            for (int i = 0; i < dt.Rows.Count; i++)
            {
                dt.Rows[i]["序号"] = (i + 1).ToString();
            }
            //增加合计
            DataRow dr = dt.NewRow();
            dr["序号"] = "汇总";
            dr["数量"] = dt.Compute("sum(数量)", "");
            dt.Rows.Add(dr);

            dataGridView1.DataSource = dt;
            dataGridView1.Refresh();
        }

        private void 刷新包含人员()
        {
            string sql = @"select  Z_UA_User.cUser_Name 报工人员,[工序名称],[不良原因大类名称] 不良大类 ,[不良原因名称] 不良原因,CAST( sum([数量])  as int) [数量]
                        from Z_报工不良原因
                        left join Z_UA_User on Z_UA_User.cUser_Id = Z_报工不良原因.质检人
                         where 1=1";
            if (dateTimePicker_ksrq.Checked == true)
            {
                sql += $"and 质检时间 > = '{dateTimePicker_ksrq.Value.ToString("yyyy-MM-dd")}'";
            }
            if (dateTimePicker_jsrq.Checked == true)
            {
                sql += $"and 质检时间 < '{dateTimePicker_jsrq.Value.AddDays(1).ToString("yyyy-MM-dd")}'";
            }
            sql += " group by [不良原因大类名称],[不良原因名称],[工序名称],Z_UA_User.cUser_Name order by 数量 desc";

            DataTable dt = SqlSugarHelp.scope.Ado.GetDataTable(sql);
            //增加合计
            DataRow dr = dt.NewRow();
            //dr["序号"] = "合计";
            dr["数量"] = dt.Compute("sum(数量)", "");
            dataGridView1.DataSource = dt;
            dataGridView1.Refresh();
        }

        private void button2_Click(object sender, EventArgs e)
        {
            SaveFileDialog saveFileDialog = new SaveFileDialog();
            saveFileDialog.Filter = "Excel文件(*.xlsx)|*.xlsx";
            saveFileDialog.FileName = $"不良原因明细表{DateTime.Now.ToString("yyMMdd")}";
            if (saveFileDialog.ShowDialog() == DialogResult.OK)
            {
                MiniExcel.SaveAs(saveFileDialog.FileName, dataGridView1.DataSource, overwriteFile: true);
                MessageBox.Show("导出成功");
            }
        }

        private void checkBox显示人员_CheckedChanged(object sender, EventArgs e)
        {
            if (checkBox显示人员.Checked == true)
            {
                刷新包含人员();
            }
            else
            {
                刷新();
            }
        }

        private void Form不良原因明细表_Load(object sender, EventArgs e)
        {
            if (checkBox显示人员.Checked == true)
            {
                刷新包含人员();
            }
            else
            {
                刷新();
            }
        }
    }
}
